new Vue({
    el: '#app',
    data() {
        return {
            fileList: [],
            tableData: [],
            form: {
                page: 1,
                limit: 15,
            },
            loading: false,
            success: 0,
            error: 0,
        }
    },
    mixins:[importMixin],
    mounted() {

    },
    methods: {
        submitUpload(i = 0) {
            const that = this;
            if (i < this.tableData.length) {
                this.loading = true;
                if (!this.tableData[i].Article_Specifications_SerialNumber) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '规格内码不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else if (!this.tableData[i].BarCode) {
                    this.tableData[i].isStatus = '2';
                    this.tableData[i].Cause = '条形码不能为空'
                    this.error++;
                    this.submitUpload(i + 1);
                } else { //校验通过
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                        type: 'POST',
                        data: {
                            Data: 'business_article_barcode',
                            Condition:"BarCode='"+that.tableData[i].BarCode+"'",
                        },
                        success: function (data) {
                            that.submitForm({ Data: JSON.stringify({ Add: { business_article_barcode: [that.tableData[i]] } }) }, (res) => {
                                if (res.Status == 'success') {
                                    that.tableData[i].isStatus = '1';
                                    if (data.length == 0) {
                                        that.tableData[i].Cause = '导入成功';
                                    } else {
                                        that.tableData[i].Cause = '导入成功，该条码已存在'+data.length+'个条码';
                                    }
                                    that.success++;
                                } else {
                                    that.tableData[i].isStatus = '2';
                                    that.tableData[i].Cause = '未知错误'
                                    that.error++;
                                }
                                that.submitUpload(i + 1);
                            })
                        },
                        error: function () {
                            that.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    });
                }
            } else if (i != 0) {
                this.loading = false;
                this.$message.success("批量导入结束")
            } else {
                this.$message({
                    message: '请先导入模板后再上传',
                    type: 'warning'
                })
            }
        },
        submitForm(data, callback) {
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalSave`,
                type: 'POST',
                data,
                success: function (res) {
                    callback(res)
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        handleChange({ raw }) {//导入监听文件
            const that = this;
            var reader = new FileReader();
            reader.readAsBinaryString(raw);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                let arr = [];
                json.forEach(item => {
                    arr.push({
                        Article_Specifications_SerialNumber:item['规格内码'],
                        BarCode:item['条形码'],
                        isEdit:false,
                        isStatus:'0',
                        Cause:''
                    })
                });
                that.tableData = arr
            }
        },
        handleSizeChange(val) {
            this.form.page = 1;
            this.form.limit = val;
        },
        handleCurrentChange(val) {
            this.form.page = val;
        },
        handleEdit(row) {//修改当前行
            row.isEdit = !row.isEdit
        },
        handleRemove(index) {//移除当前行
            this.$confirm('是否要删除此行?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = this.tableData.filter((i, id) => id != (this.form.page - 1) * this.form.limit + index)
            }).catch(() => { });
        },
        handleClean() {//清空
            this.$confirm('是否要清空导入数据?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                this.tableData = []
            }).catch(() => { });
        },
    },
})